<template>
  <div class="orders">
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card>
      <!-- 搜索添加输入框区 -->
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getOrdersList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getOrdersList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单表单页 -->
      <el-table :data="orderlist" border stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column
          label="订单价格"
          prop="order_price"
          width="80px"
        ></el-table-column>
        <el-table-column label="是否付费" prop="order_status" width="80px">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.order_status === '1'"
              >已付款</el-tag
            >
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="是否发货"
          prop="is_send"
          width="80px"
        ></el-table-column>
        <el-table-column label="下单时间" prop="create_time" width="160px">
          <template slot-scope="scope">
            {{ scope.row.create_time | dateFormat }}
          </template></el-table-column
        >
        <el-table-column label="操作" width="180px">
          <!-- 作用域插槽获取数据 -->
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-tooltip
              effect="dark"
              content="修改"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="showEditDialog(scope.row.order_id)"
              ></el-button>
            </el-tooltip>
            <!-- 定位按钮 -->
            <el-tooltip
              effect="dark"
              content="物流"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="success"
                icon="el-icon-location-information"
                size="mini"
                @click="showProgressBox"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 修改订单地址弹窗 -->
    <el-dialog
      title="修改地址"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogColsed"
    >
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            :options="cityData"
            v-model="editForm.address1"
          ></el-cascader>
        </el-form-item>

        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="editForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <!-- 对话框底部 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 展示物流进度对话框 -->
    <el-dialog
      title="物流进度"
      :visible.sync="progressDialogVisible"
      width="50%"
    >
      <h2>{{ prgressInfo.info }}</h2>
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in activities"
          :key="index"
          :timestamp="activity.timestamp"
          :icon="activity.icon"
          :type="activity.type"
          :color="activity.color"
        >
          {{ activity.content }}
        </el-timeline-item>
      </el-timeline>
      <!-- 对话框底部 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="progressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="progressDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import cityData from 'components/content/citydata.js'
export default {
  name: 'Order',
  data() {
    return {
      queryInfo: {
        //查询参数
        query: '',
        //当前页数
        pagenum: 1,
        //当前每页显示多少条数据
        pagesize: 10,
      },
      orderlist: [],
      total: 0,
      //编辑订单
      editDialogVisible: false,
      //订单修改规则
      editFormRules: {
        address1: [
          { required: true, message: '其那个选择省市区/县', trigger: 'blur' },
        ],
        address2: [
          { required: true, message: '请填写详细地址', trigger: 'blur' },
        ],
      },
      //全国地址
      cityData,
      //查询到的用户信息对象
      editForm: {
        address1: [],
        address3: '',
      },
      //物流进度显示与隐藏
      progressDialogVisible: false,
      //物流信息
      prgressInfo: [],
      //时间线
      activities: [
        {
          content: '开始发货',
          timestamp: '2021-05-15',
          type: 'success',
        },
        {
          content: '正经过这里',
          timestamp: '2021-05-16',
          type: 'primary',
          icon: 'el-icon-more',
        },
        {
          content: '预计明天送达',
          timestamp: '2021-05-17',
        },
      ],
    }
  },
  created() {
    this.getOrdersList()
  },
  computed: {},
  methods: {
    ////////////////////////////////事件监听//////////////////////
    //监听pagesize改变的事件
    handleSizeChange(newSize) {
      //   console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getOrdersList()
    },
    //监听页码值改变的事件
    handleCurrentChange(newPage) {
      //   console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getOrdersList()
    },
    //修改订单地址信息
    async showEditDialog(id) {
      const { data: res } = await this.$http.get('orders/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('查询订单信息失败！')
      }
      this.editForm = res.data
      this.editDialogVisible = true
      console.log(this.editForm)
    },
    //监听编辑订单关闭
    editDialogColsed() {
      this.$refs.editFormRef.resetFields()
    },
    showProgressBox() {
      //   const { data: res } = await this.$http.get(`/kuaidi/1106975712662`)
      //   if (res.meta.status !== 200) {
      //     return this.$message.error('获取物流信息失败！')
      //   }
      this.prgressInfo = {
        info: '后端已经停止维护，物流信息无法查询，嘻嘻',
      }
      this.progressDialogVisible = true
      //   console.log(this.prgressInfo)
    },
    ////////////////////////////////数据处理//////////////////////
    //获取订单数据列表
    async getOrdersList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo,
      })
      //   console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表失败')
      }
      this.orderlist = res.data.goods
      this.total = res.data.total
      //   console.log(res.data)
    },
    //提交修改订单预校验//后端无效（查询页无效）
    editOrderInfo() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return
        // 可以发起网络请求
        const { data: res } = await this.$http.put(
          'orders/' + this.editForm.order_id,
          {
            order_number: this.editForm.order_number,
            order_price: this.editForm.order_price,
            pay_status: this.editForm.pay_status,
            order_pay: this.editForm.order_pay,
            is_send: this.editForm.is_send,
          }
        )
        if (res.meta.status !== 200) {
          return this.$message.error('更新订单信息失败！')
        }
        //1.关闭对话框
        this.editDialogVisible = false
        //2.刷新数据列表
        this.getOrdersList()
        //3.提示修改成功
        this.$message.success('更新订单信息成功！')
      })
    },
  },
}
</script>

<style scoped>
.el-cascader {
  width: 100%;
}
.el-timeline {
  margin-top: 50px;
  font-size: 16px;
}
</style>